/**
 * @param {number[]} height
 * @return {number}
 * 
 * 输入：[1,8,6,2,5,4,8,3,7]
输出：49 
解释：图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下，容器能够容纳水（表示为蓝色部分）的最大值为 49。

 */
var maxArea = function (height) {
  // 超时
  /** let left = 0,right
  let maxWater = 0
  while(left<height.length-1){
    right=left+1
    while (right<height.length) {
      maxWater =Math.max(maxWater,Math.min(height[left],height[right])*(right-left))
      right++
    }
    left++
  }
  return maxWater
   */

  let left = 0,
    right = height.length - 1,
    maxWater = 0
  while (left < right) {
    maxWater = Math.max(
      maxWater,
      Math.min(height[left], height[right]) * (left - left)
    )
    if(height[right]<height[left])right--
    else left++
  }
  return maxWater
}

console.log(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]))
